package com.pzhu.service.impl;


import com.pzhu.dao.UserDao;
import com.pzhu.dao.impl.UserDaoImpl;
import com.pzhu.pojo.User;
import com.pzhu.service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {


    //    添加Dao层成员变量
    private final UserDao userDao = new UserDaoImpl();

    @Override
    public boolean checkUser(long account, String password) {
//      封装数据
        User user = new User(account);
        System.out.println(user.getAccount());
//      调用Dao层查询数据
        User userSelect = userDao.selectUserById(user);
        System.out.println("service checkUser方法");
//      判断数据是否匹配 如果没有数据，则直接跳过匹配
        if (userSelect != null) {
            if (userSelect.getId()==-1){
                System.out.println("service checkUser方法 Dao返回查无此人");
                return false;
            }
//            equals密码判断
            System.out.println("service checkUser方法 检查Dao返回密码是否相等");
            System.out.println(password);
            System.out.println(userSelect.getPassword());
            return password.equals(userSelect.getPassword());
        }
        System.out.println("service checkUser方法 Dao返回为空");
        return false;
    }

    @Override
    public boolean checkUserById(long id) {
        User user = new User(id);
        User userData = userDao.selectUserById(user);
        if (userData==null){
            return false;
        }
        return true;
    }

    @Override
    public String selectNameByUId(long id) {
        User user = userDao.selectUserById(new User(id));
        if (user==null){
            return "查无此人";
        }
        return user.getName();
    }

    @Override
    public User selectUserById(long id) {
        User user = userDao.selectUserById(new User(id));
        if (user==null){
            user.setId(-1);
            user.setName("查无此人");
        }
        return user;
    }


    public Long selectLastUserId() {
        List<User> users = userDao.selectAllUser();
        User user = users.get(users.size() - 1);
        System.out.println();
        if (user==null){
            return 0L;
        }
        System.out.println(user.getId());
        return user.getId();
    }

    public boolean addUser(User user) {
        long i = userDao.insertUser(user);
        if (i==-1){
            return false;
        }
        return true;
    }

    @Override
    public long selectLoginCount() {
        return userDao.selectLoginCount();
    }

    @Override
    public long setLoginCount(long logincount) {
        return userDao.setLoginCount(logincount);
    }

}
